package com.ticket.sass.provider.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ticket.sass.common.entity.tenant.ResourceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author ywb
 * @date 2022-3-22 17:46
 */
@Mapper
public interface ResourceMapper extends BaseMapper<ResourceEntity> {

    @Select(
        "SELECT   " +
        "    *   " +
        "FROM   " +
        "    resources   " +
        "WHERE   " +
        "  id   " +
        "IN    " +
        "  (     " +
        "    SELECT       " +
        "        resource_id  " +
        "    FROM  " +
        "        role_resource_maps  " +
        "    WHERE   " +
        "        role_id       " +
        "    IN (          " +
        "      SELECT          " +
        "        role_id   " +
        "      FROM  " +
        "        admin_role_maps   " +
        "      WHERE   " +
        "        admin_id = #{adminId}  " +
        "      AND   " +
        "        deleted_at IS NULL  " +
        "      )  " +
        "    AND   " +
        "      deleted_at IS NULL  " +
        "  )  " +
        "OR   " +
        "  id  " +
        "IN  " +
        "  (  " +
        "    SELECT  " +
        "      resource_id  " +
        "    FROM  " +
        "      admin_resource_maps  " +
        "    WHERE   " +
        "      admin_id = #{adminId}  " +
        "    AND   " +
        "      deleted_at IS NULL   " +
        "  )   " +
        "AND   " +
        "  deleted_at IS NULL")
    List<ResourceEntity> listByAdminId(@Param("adminId") Integer adminId);
}
